<template>
  <div class="source-search" @click="closeDropdown">
    <div class="search">
      <div class="search-input">
        <div class="filter" @click.stop="toggleDropdown">
          <div class="content">{{ selectedStation.name || '全部站点' }}</div>
          <img
            class="icon"
            :class="{ rotated: showDropdown }"
            src="/static/icon/down-arrow.svg"
            alt=""
          />
        </div>
        <div class="filter-box" v-show="showDropdown">
          <div class="header">
            <div class="title">站点筛选</div>
            <div class="clean" @click.stop="selectStation">
              <img class="icon" src="/static/icon/clean.svg" alt="" />
              清除
            </div>
          </div>
          <div class="body">
            <div
              :class="['item', { active: selectedStation.id === station.id }]"
              v-for="station in stationList"
              :key="station.id"
              @click.stop="selectStation(station)"
            >
              <div>{{ station.name }}</div>
            </div>
          </div>
        </div>
        <input
          class="input"
          type="text"
          v-model="query.content"
          placeholder="输入服务名称"
          @confirm="handleConfirm"
        />
        <img
          v-if="query.content"
          @click="handleCancel"
          class="cancel"
          src="/static/icon/cancel.svg"
          alt=""
        />
      </div>
      <div class="cancel" @click="uni.navigateBack()">取消</div>
    </div>
    <div class="search-body">
      <div class="header">
        <div class="title">历史搜索</div>
        <div class="clean" @click="cleanHistory">
          <img class="icon" src="/static/icon/clean.svg" alt="" />
          清除
        </div>
      </div>
      <div class="history">
        <div
          class="item"
          v-for="item in history"
          @click="handleHistory(item)"
          :key="item"
        >
          {{ item }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
wx.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline']
});
import { onMounted, ref } from 'vue'
import { getCategoryList } from '@/api/data.js'
const query = ref({
  content: '',
  categoryId: '',
})
const history = ref([])
const showDropdown = ref(false)
const selectedStation = ref({})
const stationList = ref([])

const toggleDropdown = () => {
  showDropdown.value = !showDropdown.value
}

const selectStation = (station) => {
  selectedStation.value = station || {}
  query.value.categoryId = station ? station.id : ''
  showDropdown.value = false
}

const closeDropdown = () => {
  showDropdown.value = false
}
const handleHistory = (item) => {
  query.value.content = item
  handleConfirm()
}
const handleCancel = () => {
  query.value.content = ''
}
const handleConfirm = () => {
  if (!query.value.content) {
    uni.showToast({
      title: '请输入搜索内容',
      icon: 'none',
    })
    return
  }
  uni.setStorageSync('service-history', [
    ...new Set([query.value.content, ...history.value]),
  ])
  uni.navigateTo({
    url: `/pages/service/service?name=${query.value.content}&categoryId=${query.value.categoryId}`,
  })
}
const cleanHistory = () => {
  uni.removeStorageSync('service-history')
  history.value = []
  uni.showToast({
    title: '清除成功',
    icon: 'none',
  })
}
onMounted(() => {
  history.value = uni.getStorageSync('service-history') || []
  getCategoryList().then((res) => {
    stationList.value = res.data.data || []
  })
})
</script>

<style lang="scss">
.search-body {
  border-top: 2rpx solid #0000000d;
  padding: 24rpx;

  .header {
    display: flex;
    justify-content: space-between;

    .title {
      font-family: PingFang SC;
      font-weight: 500;
      font-style: Medium;
      font-size: 28rpx;
    }

    .icon {
      width: 28rpx;
      height: 28rpx;
    }

    .clean {
      display: flex;
      align-items: center;
      gap: 8rpx;
      font-family: PingFang SC;
      font-weight: 400;
      font-style: Regular;
      font-size: 24rpx;
      line-height: 36rpx;
      letter-spacing: 0rpx;
      color: #969799;
    }
  }

  .history {
    margin-top: 16rpx;
    display: flex;
    flex-wrap: wrap;
    gap: 16rpx;

    .item {
      width: fit-content;
      height: 52rpx;
      padding: 0 16rpx;
      border-radius: 8rpx;
      line-height: 52rpx;
      text-align: center;
      background: #f7f8fa;
      color: #646566;
      font-family: PingFang SC;
      font-weight: 400;
      font-style: Regular;
      font-size: 24rpx;
    }
  }
}

.search {
  width: 750rpx;
  height: 88rpx;
  padding: 8rpx 24rpx;
  display: flex;
  align-items: center;
  gap: 20rpx;

  .search-input {
    width: 626rpx;
    height: 72rpx;
    border-radius: 44rpx;
    background: #f7f8fa;
    padding: 24rpx;
    display: flex;
    align-items: center;

    .filter {
      width: 160rpx;
      height: 40rpx;
      display: flex;
      align-items: center;
      gap: 8rpx;
      position: relative;
      cursor: pointer;

      .content {
        width: 112rpx;
        height: 40rpx;
        font-family: PingFang SC;
        font-weight: 400;
        font-style: Regular;
        font-size: 28rpx;
        line-height: 40rpx;
        color: #646566;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .icon {
        width: 16rpx;
        height: 16rpx;
        transition: transform 0.3s ease;

        &.rotated {
          transform: rotate(180deg);
        }
      }

      .dropdown {
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        background: white;
        border: 2rpx solid #e5e5e5;
        border-radius: 8rpx;
        box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
        z-index: 1000;
        max-height: 400rpx;
        overflow-y: auto;
        margin-top: 8rpx;

        .dropdown-item {
          padding: 16rpx 20rpx;
          font-family: PingFang SC;
          font-weight: 400;
          font-size: 28rpx;
          color: #323333;
          cursor: pointer;
          border-bottom: 2rpx solid #f5f5f5;

          &:last-child {
            border-bottom: none;
          }

          &:hover {
            background-color: #f7f8fa;
          }

          &.selected {
            background-color: #e6f7ff;
            color: #13b7a4;
          }
        }
      }

      border-right: 2rpx solid #0000000d;
    }

    .input {
      flex: 1;
      margin-left: 26rpx;
      height: 40rpx;
      font-family: PingFang SC;
      font-weight: 400;
      font-style: Regular;
      font-size: 28rpx;
      line-height: 40rpx;
      color: #323333;
    }

    .cancel {
      margin-left: 24rpx;
      width: 32rpx;
      height: 32rpx;
    }
  }

  .cancel {
    flex-shrink: 0;
    font-family: PingFang SC;
    font-weight: 400;
    font-style: Regular;
    font-size: 28rpx;
    text-align: center;
    color: #969799;
  }
}
</style>
